package com.hjq.demo.yuliao.fragment

import android.view.*
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gyf.immersionbar.ImmersionBar
import com.hjq.base.FragmentPagerAdapter
import com.hjq.demo.R
import com.hjq.demo.app.vb.AppVbActivity
import com.hjq.demo.app.vb.AppVbFragment
import com.hjq.demo.databinding.FragmentBangdanBinding
import com.hjq.demo.ui.adapter.TabAdapter
import com.hjq.demo.yuliao.adapter.HomeMainRoomListAdapter
import com.hjq.demo.yuliao.adapter.TabBangDanAdapter

/**
 *
 * @author jiahe
 * 创建日期 2022/2/8
 * 描述 首页榜单
 *
 */
class HomeBangDanFragment : AppVbFragment<AppVbActivity<*>, FragmentBangdanBinding>(), OnItemClickListener,
    TabBangDanAdapter.OnTabListener,
    ViewPager.OnPageChangeListener {

    companion object {

        fun newInstance(): HomeBangDanFragment {
            return HomeBangDanFragment()
        }
    }

    private var adapter: HomeMainRoomListAdapter? = null

    private var tabAdapter: TabBangDanAdapter? = null
    private var pagerAdapter: FragmentPagerAdapter<Fragment>? = null

    override fun initView() {
        ImmersionBar.setTitleBar(this, binding.rvBangdanTab)
        // tab
        pagerAdapter = FragmentPagerAdapter(this)
        pagerAdapter!!.addFragment(BangDanListFragment.newInstance(), "列表演示")
        pagerAdapter!!.addFragment(BangDanListFragment.newInstance(), "网页演示")
        binding.vpBangdanPager.adapter = pagerAdapter
        binding.vpBangdanPager.addOnPageChangeListener(this)
        tabAdapter = getAttachActivity()?.let { TabBangDanAdapter(it, TabAdapter.TAB_MODE_DESIGN, false) }
        binding.rvBangdanTab.adapter = tabAdapter
        tabAdapter?.addItem("贡献榜")
        tabAdapter?.addItem("魅力榜")
        tabAdapter?.setOnTabListener(this)

    }

    override fun initData() {
        getListData(true)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        getListData(false)
    }

    private fun getListData(isRefresh: Boolean) {
        val list = mutableListOf<String>()
        list.add("123456")
        list.add("123456")
        list.add("123456")
        list.add("123456")
        list.add("123456")
        list.add("123456")
        adapter?.setNewInstance(list)
    }

    /**
     * [TabAdapter.OnTabListener]
     */
    override fun onTabSelected(recyclerView: RecyclerView?, position: Int): Boolean {
        binding.vpBangdanPager.currentItem = position
        return true
    }

    /**
     * [ViewPager.OnPageChangeListener]
     */
    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

    override fun onPageSelected(position: Int) {
        tabAdapter?.setSelectedPosition(position)
    }

    override fun onPageScrollStateChanged(state: Int) {}

}